package com.google.code.jmarias.client.components;

import com.google.code.jmarias.client.Entity;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Komponenta obstaravajici animaci zajeti karet do sebe v hracove ruce
 * 
 * @author bart
 */

public class GrabCardsComponent extends AbstractRenderComponent {

    // Uhel pro vykresleni karet (180 -> zobrazen cely vejir)
    private float angle = 180f;
    // Pozice karet
    private ArrayList<Vector2f> cardsPositions;

    private ArrayList<Entity> cardsEntities;

    public GrabCardsComponent(int id, ArrayList<Vector2f> cardsPositions, ArrayList<Entity> cardsEntities) throws SlickException {
        super(id);
        this.cardsPositions = cardsPositions;
        this.cardsEntities = cardsEntities;
        
        angle = 180f;
    }


    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {
      if(delta < 1000) {

          // Animace zajeti karet
          for(int i = 0; i < cardsEntities.size(); i++) {
                if(cardsEntities.get(i).getPosition().x > cardsPositions.get(0).x) {
                    if(owner.getPosition().x - 400 * (float) Math.cos( angle * Math.PI/180f) < cardsEntities.get(i).getPosition().x) {
                          cardsEntities.get(i).setPosition(new Vector2f(owner.getPosition().x - 400 * (float) Math.cos( angle * Math.PI/180f),
                                                                        owner.getPosition().y - 300 * (float) Math.sin( angle * Math.PI/180f)));
                    }
                } else {
                    cardsEntities.get(i).setPosition(cardsPositions.get(0));
                }
          }

          // Zmenseni uhlu jak zajizdi karty do sebe
          if(angle > 0f) {
             angle = angle - 0.2f * delta;
             System.out.println(angle);
          } else {
             // Pokud jsou karty v sobe, zrusim instanci
             owner.DelComponent(id);
             return;
          }
      }
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {

        for(int i = 0; i < cardsEntities.size(); i++) {
            cardsEntities.get(i).render(gc,sb,gr);
        }

    }


}
